package com.catchman.controller;

import com.catchman.annotation.OperationLog;
import com.catchman.annotation.PermInfo;
import com.catchman.entity.SysJob;
import com.catchman.quartz.QuartzManager;
import com.catchman.service.SysJobService;
import com.catchman.vo.ResponseModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 定时任务表 前端控制器
 * </p>
 *
 * @author CatchMan
 * @since 2019-05-15
 **/
@Api(tags = {"定时任务表操作接口"})
@PermInfo(pval = "sys:job:*", value = "操作定时任务权限")
@RestController
@RequestMapping("/sysJob")
public class SysJobController {
    private static final Logger logger = LoggerFactory.getLogger(SysJobController.class);
    @Autowired
    private QuartzManager quartzManager;
    @Autowired
    private SysJobService sysJobService;

    /**
     * 查询分页数据
     */
    @ApiOperation(value = "分页查询定时任务表数据")
    @RequiresPermissions({"sys:job:list"})
    @GetMapping("/list")
    public ResponseModel findListByPage(@RequestParam(name = "current", defaultValue = "1") int current,
                                        @RequestParam(name = "size", defaultValue = "20") int size) {
        return null;
    }


    /**
     * 根据id查询
     */
    @ApiOperation(value = "根据id查询定时任务表数据")
    @RequiresPermissions({"sys:job:query"})
    @GetMapping("/getById")
    public ResponseModel getById(@RequestParam("pkid") String pkid) {
        return null;
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增定时任务表数据")
    @RequiresPermissions({"sys:job:add"})
    @PutMapping("/add")
    public ResponseModel add(@RequestBody SysJob sysJob) {
        return null;
    }

    /**
     * 批量删除
     */
    @ApiOperation(value = "批量删除定时任务表数据")
    @RequiresPermissions({"sys:job:delete"})
    @DeleteMapping("/del")
    public ResponseModel delete(@RequestParam("ids") List<String> ids) {
        return null;
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新定时任务表数据")
    @RequiresPermissions({"sys:job:update"})
    @PostMapping("/update")
    public ResponseModel update(@RequestBody SysJob sysJob) {
        return null;
    }

    @Autowired
    EhCacheManager ehCacheManager;

    @ApiOperation("启动定时任务")
    @OperationLog("启动定时任务")
    @RequiresPermissions("sys:job:start")
    @PostMapping("/startJob")
    public ResponseModel startJob(String id) {
        Object o =ehCacheManager.getCache("shiroCache");
        System.out.println(o);
        try {
            SysJob job = sysJobService.getById(id);
            quartzManager.addJob(job.getJobName(), job.getGroupName(), job.getClazzPath(), job.getCron());
            //修改状态
            job.setStatus(true);
            sysJobService.updateById(job);
        } catch (Exception e) {
            logger.error("{}", e.getLocalizedMessage());
            e.printStackTrace();
            return ResponseModel.fail().msg(e.getLocalizedMessage());
        }
        return ResponseModel.succ();
    }

    @ApiOperation("停止定时任务")
    @OperationLog("停止定时任务")
    @RequiresPermissions("sys:job:end")
    @PostMapping("/endJob")
    public ResponseModel endJob(String id) {
        try {
            SysJob job = sysJobService.getById(id);
            quartzManager.removeJob(job.getJobName(), job.getGroupName());
            //修改状态
            job.setStatus(false);
            sysJobService.updateById(job);
        } catch (Exception e) {
            logger.error("{}", e.getLocalizedMessage());
            return ResponseModel.fail().msg(e.getLocalizedMessage());
        }
        return ResponseModel.succ();
    }

    @ApiOperation("暂停定时任务")
    @OperationLog("暂停定时任务")
    @RequiresPermissions("sys:job:pause")
    @PostMapping("/pauseJob")
    public ResponseModel pauseJob(String id) {
        try {
            SysJob job = sysJobService.getById(id);
            quartzManager.pauseJob(job.getJobName(), job.getGroupName());
        } catch (Exception e) {
            logger.error("{}", e.getLocalizedMessage());
            return ResponseModel.fail(e.getLocalizedMessage());
        }
        return ResponseModel.succ();
    }

    @ApiOperation("恢复定时任务")
    @OperationLog("恢复定时任务")
    @RequiresPermissions("sys:job:resume")
    @PostMapping("/resumeJob")
    public ResponseModel resumeJob(String id) {
        try {
            SysJob job = sysJobService.getById(id);
            quartzManager.resumeJob(job.getJobName(), job.getGroupName());
        } catch (Exception e) {
            logger.error("{}", e.getLocalizedMessage());
            return ResponseModel.fail().msg(e.getLocalizedMessage());
        }
        return ResponseModel.succ();
    }

}

